//
//  UIView+Tap.m
//  Rabbit
//
//  Created 阿帕 on 2019/12/20.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "UIView+Tap.h"

#import <objc/runtime.h>

const char *blockKey = "blockKey";

@implementation UIView (Tap)

- (void)setTapBlock:(TapBlock)tapBlock {
    objc_setAssociatedObject(self, &blockKey, tapBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (TapBlock)tapBlock {
    return objc_getAssociatedObject(self, &blockKey);
}

- (void)nyl_addTapGes:(TapBlock)block {
    self.tapBlock = block; // 把block赋值给self.tapBlock, 可以在当前函数之外执行该回调
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes)];
    [self addGestureRecognizer:tap];
}

- (void)tapGes {
    if (self.tapBlock) {
        self.tapBlock(); // 执行回调
    }
}

@end
